package com.light.gulimall.ware.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class MyMQConfig {

//    /**
//     * 队列，交换机是懒加载的，只有第一次监听消息发现不存在的时候才会创建
//     * @param message
//     * @param channel
//     */
//    @RabbitListener
//    public void listener(Message message, Channel channel){
//
//    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public Exchange stockEventExchange() {
        return new TopicExchange("stock-event-exchange", true, false);
    }

    /**
     * 延迟队列 50分钟
     * @return
     */
    @Bean
    public Queue stockDelayQueue() {
        HashMap<String, Object> map = new HashMap<>();
        //死信交换机
        map.put("x-dead-letter-exchange", "stock-event-exchange");
        //路由键
        map.put("x-dead-letter-routing-key", "stock.release");
        //过期时间
        map.put("x-message-ttl", 120000);
        return new Queue("stock.delay.queue", true, false, false, map);
    }

    /**
     * 解锁库存消息队列
     * @return
     */
    @Bean
    public Queue stockReleaseQueue() {
        return new Queue("stock.release.stock.queue", true, false, false);
    }

    /**
     * 将延迟队列绑定到交换机
     * @return
     */
    @Bean
    public Binding delayQueueToExchange() {
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }

    /**
     * 解锁库存消息队列绑定到交换机
     * @return
     */
    @Bean
    public Binding stockReleaseToExchange() {
        return new Binding(
                "stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",
                null
        );
    }


}
